package com.hooker.penetration

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hooker.adapter.TypeFilterAdapter
import com.hooker.bean.FilterInfo
import com.hooker.bean.TypeInfo
import com.hooker.helper.FilterEnum
import com.hooker.settings.SettingHelper
import com.sdk.hooker.penetration.R

class TypeFilterActivity : AppCompatActivity() {

    var info: TypeInfo? = null
    /**
     * 包名
     */
    var pName: String? = null
    lateinit var filterView: RecyclerView
    lateinit var adapter: TypeFilterAdapter
    var hookData: MutableList<FilterInfo> = mutableListOf()

    companion object {
        var categorys: MutableList<String> = mutableListOf()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_filter)

        info = intent?.getParcelableExtra("extra")
        pName = intent?.getStringExtra("package")

        initView()
        initData()
    }

    private fun initView() {
        title = info?.id

        filterView = findViewById(R.id.filter_view)
        filterView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        filterView.overScrollMode = View.OVER_SCROLL_NEVER
    }

    private fun initData() {
        categorys.clear()
        var iterator = FilterEnum.CategoryType.values().iterator()
        while (iterator.hasNext()) {
            var info = iterator.next()
            categorys.add(info.name)
        }

        for (category in categorys) {
            var includeList =
                //pName_Hooker.File_path_include
                SettingHelper.getInfo(
                    applicationContext,
                    SettingHelper.getBuildInfo(
                        pName,
                        info?.id,
                        category,
                        FilterEnum.FilterType.INCLUDE.name
                    )
                )
            var startList =
                SettingHelper.getInfo(
                    applicationContext,
                    SettingHelper.getBuildInfo(
                        pName,
                        info?.id,
                        category,
                        FilterEnum.FilterType.START.name
                    )
                )

            var endList =
                SettingHelper.getInfo(
                    applicationContext,
                    SettingHelper.getBuildInfo(
                        pName,
                        info?.id,
                        category,
                        FilterEnum.FilterType.END.name
                    )
                )

            var regexList =
                //pName_Hooker.File_path_regex
                SettingHelper.getInfo(
                    applicationContext,
                    SettingHelper.getBuildInfo(
                        pName,
                        info?.id,
                        category,
                        FilterEnum.FilterType.REGEX.name
                    )
                )

            var info = FilterInfo(category, pName)
            info.includes = includeList
            info.starts = startList
            info.ends = endList
            info.regexs = regexList

            hookData.add(info)
        }

        info?.packageName = pName
        adapter = TypeFilterAdapter(this, hookData, filterView, info)
        filterView.adapter = adapter
    }
}